package me.sdevil507.systemAssist.systemLog.controller;

import lombok.extern.slf4j.Slf4j;
import me.sdevil507.platform.dto.PlatResourceResultDto;
import me.sdevil507.platform.po.PlatResourcePo;
import me.sdevil507.platform.service.PlatResourceService;
import me.sdevil507.supports.jpa.PageConvertCallBack;
import me.sdevil507.supports.jpa.PageResultDTO;
import me.sdevil507.supports.jpa.PageResultGenerator;
import me.sdevil507.supports.result.ApiResultDTO;
import me.sdevil507.supports.result.ApiResultGenerator;
import me.sdevil507.supports.shiro.helper.OrgAccountHelper;
import me.sdevil507.systemAssist.systemLog.dto.SystemLogMergeParamsDto;
import me.sdevil507.systemAssist.systemLog.dto.SystemOperLogResultDto;
import me.sdevil507.systemAssist.systemLog.mapper.SystemErrorLogMapper;
import me.sdevil507.systemAssist.systemLog.mapper.SystemOperLogMapper;
import me.sdevil507.systemAssist.systemLog.po.SystemAccessLogPo;
import me.sdevil507.systemAssist.systemLog.po.SystemErrorLogPo;
import me.sdevil507.systemAssist.systemLog.po.SystemOperLogPo;
import me.sdevil507.systemAssist.systemLog.service.SystemAccessLogService;
import me.sdevil507.systemAssist.systemLog.service.SystemErrorLogService;
import me.sdevil507.systemAssist.systemLog.service.SystemOperLogService;
import me.sdevil507.systemAssist.systemLog.dto.SystemErrorLogResultDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * 日志Controller
 *
 */
@RestController
@RequestMapping("/api/plat/systemLog")
@Slf4j
public class SystemLogController {

    @Autowired
    private SystemOperLogService systemOperLogService;
    @Autowired
    private SystemOperLogMapper systemOperLogMapper;
    @Autowired
    private SystemErrorLogService systemErrorLogService;
    @Autowired
    private SystemErrorLogMapper systemErrorLogMapper;
    @Autowired
    private SystemAccessLogService systemAccessLogService;
    @Autowired
    private PlatResourceService platResourceService;

    @PostMapping("/v1/readAllOperByPage")
    public ApiResultDTO readAllOperByPage(@RequestBody SystemLogMergeParamsDto paramsDto) {
        ApiResultDTO apiResultDto;
        try {
            Page<SystemOperLogPo> pagePos = systemOperLogService.readAllByPage(paramsDto.getOperLogParamsDto(), paramsDto.getPageDto(), paramsDto.getSortDto());

            PageResultDTO<SystemOperLogResultDto> pageResultDto = (PageResultDTO<SystemOperLogResultDto>) PageResultGenerator.convert(pagePos, new PageConvertCallBack<SystemOperLogResultDto, SystemOperLogPo>() {
                @Override
                public List<SystemOperLogResultDto> exec(List<SystemOperLogPo> list) {
                    return systemOperLogMapper.pos2dtos(list);
                }
            });
            apiResultDto = ApiResultGenerator.create("0", "分页数据获取成功!", pagePos);
        } catch (Exception e) {
            log.error("分页数据获取失败:", e);
            e.printStackTrace();
            apiResultDto = ApiResultGenerator.create("-1", "分页数据获取失败!");
        }
        return apiResultDto;
    }

    @PostMapping("/v1/readAllErrorByPage")
    public ApiResultDTO readAllErrorByPage(@RequestBody SystemLogMergeParamsDto paramsDto) {
        ApiResultDTO apiResultDto;
        try {
            Page<SystemErrorLogPo> pagePos = systemErrorLogService.readAllByPage(paramsDto.getErrorLogParamsDto(), paramsDto.getPageDto(), paramsDto.getSortDto());

            PageResultDTO<SystemErrorLogResultDto> pageResultDto = (PageResultDTO<SystemErrorLogResultDto>) PageResultGenerator.convert(pagePos, new PageConvertCallBack<SystemErrorLogResultDto, SystemErrorLogPo>() {
                @Override
                public List<SystemErrorLogResultDto> exec(List<SystemErrorLogPo> list) {
                    return systemErrorLogMapper.pos2dtos(list);
                }
            });
            apiResultDto = ApiResultGenerator.create("0", "分页数据获取成功!", pagePos);
        } catch (Exception e) {
            log.error("分页数据获取失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "分页数据获取失败!");
        }
        return apiResultDto;
    }

    @PostMapping("/access/create")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO createAccessLog(@RequestBody SystemAccessLogPo systemAccessLogPo) {
        ApiResultDTO apiResultDto;
        try {
            systemAccessLogPo.setUserId(OrgAccountHelper.getUserId());
            systemAccessLogService.add(systemAccessLogPo);
            apiResultDto = ApiResultGenerator.create("0", "数据新增成功!");
        }catch (Exception e){
            log.error("分页数据获取失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "数据新增失败!");
        }
        return apiResultDto;
    }

    /**
     * 获取最近使用模块
     * @param systemAccessLogPo
     * @return
     */
    @PostMapping("/access/getRecentModule")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO getRecentModule(@RequestBody SystemAccessLogPo systemAccessLogPo) {
        ApiResultDTO apiResultDto;
        try {
            systemAccessLogPo.setUserId(OrgAccountHelper.getUserId());
            List<String> systemAccessLogPaths = systemAccessLogService.getRecentModule(systemAccessLogPo);
            List<PlatResourceResultDto> result = platResourceService.findAllByPathList(systemAccessLogPaths);
            apiResultDto = ApiResultGenerator.create("0", "数据获取成功!", result);
        }catch (Exception e){
            log.error("分页数据获取失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "数据获取失败!");
        }
        return apiResultDto;
    }
}
